﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：徐毅
    //
    // 文件名：OrderAllProductList.cs
    // 文件功能描述：所有采购单品明细列表 
    // 创建标识：徐毅2009-05-21
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.OrderManage
{
    public partial class OrderAllProductList : BasePage
    {
        public OrderAllProductList()
        {
            this.ModuleNo = Module.订单管理.ToString();
        }

        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据 
           
                txtProductName.Text = "";
                txtOutLookc.Text = "";
                txtStartDate.Text = "";
                txtEndDate.Text = "";
                txtHospitalName.Text = "";
                txtDetailOrderName.Text = "";
                //txtBatchRecordID.Text = "";
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        public void Bind()
        {
            HZ.DrugTrade.Model.Query.PurchaseOrderDetail QueryDistribution = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();

            QueryDistribution.StartTimeFrom = new HZ.Common.ConvertHelper(txtStartDate.Text.Trim()).ToDateTime;
            QueryDistribution.StartTimeTo = new HZ.Common.ConvertHelper(txtEndDate.Text.Trim()).ToDateTime;
            //QueryDistribution.Append = " NVL(OrderDetailState,0)<>0 ";
            switch (ddlStatus.SelectedItem.Text)
            {

                case "未确认":
                    QueryDistribution.OrderDetailState = 1;
                    break;
                case "已确认":
                    QueryDistribution.OrderDetailState = 2;
                    break;
                case "拒绝配送":
                    QueryDistribution.OrderDetailState = 3;
                    break;
                case "全部":
                    QueryDistribution.Append = "  (OrderDetailState=1 or OrderDetailState=2 or OrderDetailState=3)";
                    break;
            }
            QueryDistribution.HospitalName = this.txtHospitalName.Text.Trim();
            QueryDistribution.OrderName = this.txtDetailOrderName.Text.Trim();
            QueryDistribution.GoodsName = this.txtProductName.Text.Trim();
            QueryDistribution.Outlookc = this.txtOutLookc.Text.Trim();
            QueryDistribution.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;

            QueryDistribution.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC ";
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalrecord = 0;
            try
            {
                IList<HZ.DrugTrade.Model.View.PurchaseOrderDetail> vDistributionList = iPurchase.GetPurchaseOrderDetailByWhere(QueryDistribution, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
                gvwProduceCatalog.AutoGenerateColumns = false;
                gvwProduceCatalog.DataSource = vDistributionList;
                gvwProduceCatalog.DataBind();
                AspNetPager1.RecordCount = totalrecord;
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //0未确认;1已确认;
                Literal ltr = e.Row.FindControl("ltrStatus") as Literal;
                //if (ltr.Text == "0")
                //{
                //    ltr.Text = "医疗机构未提交";
                //}
                if (ltr.Text == "1")
                {
                    ltr.Text = "未确认";
                }
                else if (ltr.Text == "2")
                {
                    ltr.Text = "已确认";
                }
                else if (ltr.Text == "3")
                {
                    ltr.Text = "拒绝配送";
                }
            }
        }
        //确认配送
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            publicStuate(2);
        }
        //拒绝配送
        protected void btnConfirm0_Click(object sender, EventArgs e)
        {
            publicStuate(3);
        }

        /// <summary>
        /// 公共方法
        /// </summary>
        /// <param name="nStasta">2 确认 3 拒绝配送</param>
        private void publicStuate(int nStasta)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();

            if (Request.Form["checkbox1"] != null)
            {
                string[] strCheck1 = (Request.Form["checkbox1"].ToString()).Split(',');
                for (int i = 0; i < strCheck1.Length; i++)
                {
                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                    purchaseOrderDetail.OrderDetailID = strCheck1[i].ToString();
                    myList.Add(purchaseOrderDetail);
                }
            }
            if (myList.Count > 0)
            {
                int flag = 0;
                if (nStasta == 2)
                {
                    flag = iPurchase.ConfirmPurchaseOrderDetail_Batch(myList);
                    
                }
                else
                {
                    flag = iPurchase.RefuseConfirmPurchaseOrderDetail_Batch(myList);
                    
                }
                if (flag == 1)
                {
                    HZ.Web.WebUtil.Alert("确认成功！",this);
                    Bind();
                }
                else
                {
                    //PageError("确认失败！", "");
                    HZ.Web.WebUtil.Alert("确认失败！", this);
                    
                }
            }
        }

        
    }
}
